package com.javasm.crm.service.impl.productimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.ProductCriteria;
import com.javasm.crm.domin.entity.productEntity.ProductInfo;
import com.javasm.crm.domin.entity.productEntity.ProductVo;
import com.javasm.crm.mapper.goodsmapper.ProductVoMapper;
import com.javasm.crm.service.productservice.ProductVoService;
import com.javasm.crm.utils.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Author：zhangyang
 * @Version：1.0
 * @Date：2021/7/12-17:00
 * @desp:
 * @since:
 */
@Service
@Transactional
public class ProductVoServiceImpl extends ServiceImpl<ProductVoMapper, ProductVo> implements ProductVoService {
    @Autowired
    ProductVoMapper productVoMapper;
    @Override
    public PageResult<ProductVo> searchPageByCriteria(ProductCriteria productCriteria) {
        Integer currentPage = productCriteria.getCurrentPage();

        Integer pageSize = productCriteria.getPageSize();
        if (StringUtils.isEmpty(currentPage)) {
            currentPage=1;
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize=5;
        }
        QueryWrapper<ProductVo> exampleQueryWrapper = new QueryWrapper<>();

        exampleQueryWrapper.eq("p.deleted",0);
        if (!StringUtils.isEmpty(productCriteria.getProductId())){
            exampleQueryWrapper.eq("p.product_id",productCriteria.getProductId());
        }
        if (!StringUtils.isEmpty(productCriteria.getProductModel())) {
            exampleQueryWrapper.like("p.product_model", productCriteria.getProductModel());
        }
        if (!StringUtils.isEmpty(productCriteria.getBrandId())) {
            exampleQueryWrapper.eq("pb.brand_id", productCriteria.getBrandId());
        }
        if (!StringUtils.isEmpty(productCriteria.getColorId())) {
            exampleQueryWrapper.eq("pc.color_id", productCriteria.getColorId());
        }
        if (!StringUtils.isEmpty(productCriteria.getTypeId())) {
            exampleQueryWrapper.eq("pt.type_id", productCriteria.getTypeId());
        }
        Page<ProductVo> examplePage = new Page<>(currentPage, pageSize);
        List<ProductVo> queryall = productVoMapper.queryall(examplePage, exampleQueryWrapper);

        return new PageResult<ProductVo>(examplePage.getTotal(), queryall);
    }

    @Override
    public int deleteById(Serializable productId) {
        return productVoMapper.deleteById(productId);
    }

    @Override
    public int saveProduct(ProductVo productVo) {
        return productVoMapper.insert(productVo);
    }

    @Override
    public ProductInfo findById(Serializable productId) {
        return productVoMapper.selectById(productId);
    }

}
